//
//  Expression.m
//  解释器模式
//
//  Created by flannery on 2018/12/19.
//  Copyright © 2018年 flannery. All rights reserved.
//

#import "Expression.h"

@implementation Expression
-(BOOL)interpret:(NSString*)context{return NO;}
@end

@interface TerminalExpression ()
@property (nonatomic, strong) NSString* data;
@end
@implementation TerminalExpression
-(instancetype)initWithExpression:(NSString*)data{
    if(self = [super init]) {
        self.data = data;
    }
    return self;
}

-(BOOL)interpret:(NSString*)context{
    if([context containsString:self.data]) {
        return YES;
    }
    return NO;
}
@end


@interface OrExpression ()
@property (nonatomic, strong) Expression* expr1;
@property (nonatomic, strong) Expression* expr2;
@end
@implementation OrExpression
-(instancetype)initWithExpression1:(Expression*)expr1 expression2:(Expression*)expr2{
    if(self = [super init]) {
        self.expr1 = expr1;
        self.expr2 = expr2;
    }
    return self;
}

-(BOOL)interpret:(NSString*)context{
    return [self.expr1 interpret: context] || [self.expr2 interpret:context];
}
@end

@interface AndExpression ()
@property (nonatomic, strong) Expression* expr1;
@property (nonatomic, strong) Expression* expr2;
@end
@implementation AndExpression
-(instancetype)initWithExpression1:(Expression*)expr1 expression2:(Expression*)expr2{
    if(self = [super init]) {
        self.expr1 = expr1;
        self.expr2 = expr2;
    }
    return self;
}

-(BOOL)interpret:(NSString*)context{
    return [self.expr1 interpret:context] && [self.expr2 interpret:context];
}
@end
